package v1

import (
	"fmt"
	"usplatformserver/global"
	"usplatformserver/internal/service"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/errcode"

	"github.com/gin-gonic/gin"
)

type ProductUsageRate struct{}

func NewProductUsageRate() ProductUsageRate {
	return ProductUsageRate{}
}

// @Summer 获取产品使用率数据
// @Produce json
// @Param token header string true "token"
// @Param beginTime query string false "起始时间"
// @Param endTime query string false "截止时间"
// @Param page query int false "页码"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} model.ProductUsageDataListSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/product/usage [get]
func (p *ProductUsageRate) GetProductUsageRateData(c *gin.Context) {
	param := service.ProductUsageRateRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	fmt.Print("beginTime: ", param.BeginTime, " endTime: ", param.EndTime)

	svc := service.New(c.Request.Context())
	//获取患者列表
	paramPatientList := service.PatientListRequest{}
	paramPatientList.BeginTime = param.BeginTime
	paramPatientList.EndTime = param.EndTime
	patientList, err := svc.ListWithoutTypeAndPager(&paramPatientList)
	if err != nil {
		// 返回错误
		response.ToErrorResponse(errcode.ErrorGetProductAccountListFail)
		global.Logger.Infof("ListWithoutType failed: %v", err)
		return
	}

	count, err := svc.CountWithoutType(&paramPatientList)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetProductAccountListFail)
		global.Logger.Infof("CountWithoutType failed: %v", err)
		return
	}

	if len(patientList) == 0 {
		response.ToResponseList(nil, 0)
		return
	}

	var patientIds string
	for _, v := range patientList {
		if len(patientIds) == 0 {
			patientIds += fmt.Sprintf("%v", v.ID)
		} else {
			patientIds += fmt.Sprintf(",%v", v.ID)
		}
	}
	global.Logger.Infof("GetProductUsageRateData patientIds: %v", patientIds)

	//获取使用率数据
	usageData, err := svc.GetUsageData(patientIds, param.BeginTime, param.EndTime, count)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetProductAccountListFail)
		global.Logger.Infof("GetUsageData failed: %v", err)
		return
	}
	// 返回数据
	response.ToResponseList(usageData, count)
}

// @Summer 导出产品使用率数据
// @Produce json
// @Param token header string true "token"
// @Param beginTime query string false "起始时间"
// @Param endTime query string false "截止时间"
// @Success 200 {object} model.ProductUsageDataListSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/product/usage/export [get]
func (p *ProductUsageRate) ExportProductUsageRateData(c *gin.Context) {
	param := service.ProductUsageRateRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	fmt.Print("beginTime: ", param.BeginTime, " endTime: ", param.EndTime)

	svc := service.New(c.Request.Context())

	//获取患者列表
	paramPatientList := service.PatientListRequest{}
	paramPatientList.BeginTime = param.BeginTime
	paramPatientList.EndTime = param.EndTime
	patientList, err := svc.ListWithoutTypeAndPager(&paramPatientList)
	if err != nil {
		// 返回错误
		response.ToErrorResponse(errcode.ErrorGetProductAccountListFail)
		global.Logger.Infof("ListWithoutType failed: %v", err)
		return
	}

	//导出数据为空，直接回复，前端需要判断
	if len(patientList) == 0 {
		response.ToResponseList(nil, 0)
		return
	}

	count, err := svc.CountWithoutType(&paramPatientList)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetProductAccountListFail)
		global.Logger.Infof("CountWithoutType failed: %v", err)
		return
	}

	var patientIds string
	for _, v := range patientList {
		if len(patientIds) == 0 {
			patientIds += fmt.Sprintf("%v", v.ID)
		} else {
			patientIds += fmt.Sprintf(",%v", v.ID)
		}
	}
	global.Logger.Infof("ExportProductUsageRateData patientIds: %v", patientIds)

	//获取使用率数据
	usageData, err := svc.GetUsageData(patientIds, param.BeginTime, param.EndTime, count)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetProductAccountListFail)
		global.Logger.Infof("GetUsageData failed: %v", err)
		return
	}

	//获取使用率数据
	fileName, err := svc.ExportUsageData(usageData)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorExportProductUsageDataFail)
		global.Logger.Infof("ExportUsageData failed: %v", err)
		return
	}
	//回复下载
	response.ToResponseDownload(fileName)
}
